package org.jjj.org.controller;

import org.jjj.org.dto.AuditDTO;
import org.jjj.org.service.IShopService;
import org.jjj.org.domain.Shop;
import org.jjj.org.query.ShopQuery;
import org.jjj.basic.utils.ResultAjax ;
import org.jjj.basic.utils.PageList ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return ResultAjax转换结果
     */
    @PutMapping
    public ResultAjax addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.add(shop);
            return ResultAjax.me();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultAjax.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public ResultAjax delete(@PathVariable("id") Long id){
        try {
            shopService.deleteById(id);
            return ResultAjax.me();
        } catch (Exception e) {
        e.printStackTrace();
            return ResultAjax.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public ResultAjax get(@PathVariable("id")Long id)
    {
        try {
            Shop shop = shopService.selectById(id);
            return ResultAjax.me().setResult(shop);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultAjax.me().setSuccess(false).setMessage("获取一个失败！"+e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public ResultAjax list(){

        try {
            List< Shop> list = shopService.selectAll();
            return ResultAjax.me().setResult(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultAjax.me().setSuccess(false).setMessage("获取所有失败！"+e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public ResultAjax json(@RequestBody ShopQuery query)
    {
        try {
            PageList<Shop> pageList = shopService.queryPage(query);
            return ResultAjax.me().setResult(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultAjax.me().setSuccess(false).setMessage("获取分页数据失败！"+e.getMessage());
        }
    }

    /**
     * 店铺入住平台
     * @param shop
     * @return
     */
    @PostMapping("/settlement")
    public ResultAjax settlement(@RequestBody Shop shop)
    {
        try {
            shopService.settlement(shop);
            return ResultAjax.me();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultAjax.me().setSuccess(false).setMessage("店铺入住失败！"+e.getMessage());
        }
    }

    /**
     * 激活店铺
     * @param id
     * @return
     */
    @GetMapping("/active/{id}")
    public ResultAjax active(@PathVariable("id")Long id)
    {
        try {
            shopService.active(id);
            return ResultAjax.me();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultAjax.me().setSuccess(false).setMessage("店铺激活失败！"+e.getMessage());
        }
    }

    /**
     * 人工审核
     * @param dto
     * @return
     */
    @PutMapping("/audit")
    public ResultAjax manualReview(@RequestBody AuditDTO dto){
        try {
            shopService.manualReview(dto);
            return ResultAjax.me();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultAjax.me().setMessage("审核提交失败！"+e.getMessage());
        }
    }
}
